<template>
  <div class="app-container">
    <el-form :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      class="form-search"
      label-width="170px">
      <el-form-item :label="$t('peyee.beneName')"
        prop="beneName">
        <el-input v-model="queryParams.beneName"
          :placeholder="$t('common.placeholderContent')"
          clearable
          style="width: 240px"
          maxlength="50"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item :label="$t('peyee.beneAccountNumber')"
        prop="beneAccountNumber">
        <el-input v-model="queryParams.beneAccountNumber"
          :placeholder="$t('common.placeholderContent')"
          clearable
          style="width: 240px"
          maxlength="50"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item :label="$t('peyee.debitAccountNo')"
        prop="debitAccountNo">
        <el-input v-model="queryParams.debitAccountNo"
          :placeholder="$t('common.placeholderContent')"
          clearable
          style="width: 240px"
          maxlength="50"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item :label="$t('peyee.ifscCode')"
        prop="ifscCode">
        <el-input v-model="queryParams.ifscCode"
          :placeholder="$t('common.placeholderContent')"
          clearable
          style="width: 240px"
          maxlength="50"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item :label="$t('payrollO.transStatus')"
        prop="transStatus">
        <el-select v-model="queryParams.transStatus"
          :placeholder="$t('common.placeholderSelect')"
          clearable
          style="width: 240px">
          <el-option v-for="dict in dict.type.trans_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery">{{$t('common.search')}}</el-button>
        <el-button icon="el-icon-refresh"
          size="mini"
          @click="resetQuery">{{$t('common.reset')}}</el-button>
      </el-form-item>
    </el-form>

    <div class="section-box">
      <el-row :gutter="10"
        class="mb8">
        <!-- <el-col :span="1.5">
        <el-button type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd">{{$t('common.add')}}</el-button>
      </el-col> -->
        <!-- <el-col :span="1.5">
          <el-button type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate">{{$t('common.edit')}}</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete">{{$t('common.delete')}}</el-button>
        </el-col> -->
        <el-col :span="1.5">
          <el-button type="warning"
            plain
            icon="el-icon-close"
            size="mini"
            @click="handleClose">{{$t('common.close')}}</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch"
          @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading"
        :data="peyeeList">
        <!-- <el-table-column type="selection"
          width="55"
          align="center" /> -->

        <el-table-column :label="$t('peyee.beneName')"
          align="center"
          prop="beneName"
          min-width="160"
          show-overflow-tooltip />
        <el-table-column :label="$t('peyee.beneShortName')"
          align="center"
          prop="beneShortName"
          :show-overflow-tooltip="true"
          min-width="140" />
        <el-table-column :label="$t('peyee.beneAccountNumber')"
          align="center"
          prop="beneAccountNumber"
          :show-overflow-tooltip="true"
          min-width="160" />
        <el-table-column :label="$t('peyee.paymentMode')"
          align="center"
          prop="paymentMode"
          :show-overflow-tooltip="true"
          min-width="160" />
        <el-table-column :label="$t('peyee.ifscCode')"
          align="center"
          prop="ifscCode"
          :show-overflow-tooltip="true"
          min-width="160" />
        <el-table-column :label="$t('peyee.amount')"
          align="center"
          prop="amount"
          min-width="120" />
        <el-table-column :label="$t('peyee.transAmount')"
          align="center"
          prop="transAmount"
          min-width="120" />
        <el-table-column :label="$t('payrollO.transStatus')"
          align="center"
          prop="transStatus"
          :show-overflow-tooltip="true"
          min-width="140">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.trans_status"
              :value="scope.row.transStatus" />
          </template>
        </el-table-column>
        <el-table-column :label="$t('peyee.exchangeName')"
          align="center"
          prop="exchangeName"
          min-width="160"
          show-overflow-tooltip />
        <el-table-column :label="$t('peyee.status')"
          align="center"
          min-width="100">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.status"
              active-value="1"
              inactive-value="0"
              @change="handleStatusChange(scope.row)"></el-switch>
          </template>
        </el-table-column>
        <el-table-column :label="$t('peyee.mobileNumber')"
          align="center"
          prop="mobileNumber"
          min-width="160"
          show-overflow-tooltip />
        <el-table-column :label="$t('peyee.emailId')"
          align="center"
          prop="emailId"
          min-width="160"
          show-overflow-tooltip />
        <el-table-column :label="$t('common.remark')"
          prop="remark"
          min-width="160"
          show-overflow-tooltip />
        <el-table-column :label="$t('common.controls')"
          align="center"
          class-name="small-padding fixed-width"
          fixed="right"
          min-width="140"
          v-if="auditStatus != '1'">
          <template slot-scope="scope">
            <el-button size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)">{{$t('common.edit')}}</el-button>
            <el-button size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)">{{$t('common.delete')}}</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList" />
    </div>

    <!-- 添加或修改角色配置对话框 -->
    <el-dialog :title="title"
      :visible.sync="open"
      width="600px"
      append-to-body>
      <el-form ref="form"
        :model="form"
        :rules="rules"
        label-width="160px">
        <el-form-item :label="$t('peyee.exchangeName')"
          prop="exchangeName">
          <el-input v-model="form.exchangeName"
            maxlength="50"
            :placeholder="$t('common.placeholderContent')" />
        </el-form-item>
        <el-form-item :label="$t('peyee.beneName')"
          prop="beneName">
          <el-input v-model="form.beneName"
            maxlength="50"
            :placeholder="$t('common.placeholderContent')" />
        </el-form-item>
        <el-form-item :label="$t('peyee.beneShortName')"
          prop="beneShortName">
          <el-input v-model="form.beneShortName"
            maxlength="50"
            :placeholder="$t('common.placeholderContent')" />
        </el-form-item>
        <el-form-item :label="$t('peyee.beneAccountNumber')"
          prop="beneAccountNumber">
          <el-input v-model="form.beneAccountNumber"
            maxlength="50"
            :placeholder="$t('common.placeholderContent')" />
        </el-form-item>
        <el-form-item :label="$t('peyee.bankName')"
          prop="bankName">
          <el-input v-model="form.bankName"
            maxlength="50"
            :placeholder="$t('common.placeholderContent')" />
        </el-form-item>
        <el-form-item :label="$t('peyee.ifscCode')"
          prop="ifscCode">
          <el-input v-model="form.ifscCode"
            maxlength="50"
            :placeholder="$t('common.placeholderContent')" />
        </el-form-item>
        <el-form-item :label="$t('payrollO.transStatus')"
          prop="transStatus">
          <el-select v-model="form.transStatus"
            :placeholder="$t('common.placeholderSelect')"
            clearable
            disabled
            style="width: 100%">
            <el-option v-for="dict in dict.type.trans_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('peyee.mobileNumber')"
          prop="mobileNumber">
          <el-input v-model="form.mobileNumber"
            maxlength="50"
            :placeholder="$t('common.placeholderContent')" />
        </el-form-item>
        <el-form-item :label="$t('peyee.emailId')"
          prop="emailId">
          <el-input v-model="form.emailId"
            maxlength="50"
            :placeholder="$t('common.placeholderContent')" />
        </el-form-item>
        <el-form-item :label="$t('peyee.transAmount')"
          prop="transAmount">
          <el-input v-model="form.transAmount"
            maxlength="50"
            :placeholder="$t('common.placeholderContent')" />
        </el-form-item>
        <el-form-item :label="$t('peyee.paymentMode')"
          prop="paymentMode">
          <el-input v-model="form.paymentMode"
            maxlength="50"
            :placeholder="$t('common.placeholderContent')" />
        </el-form-item>
        <!-- <el-form-item :label="$t('peyee.status')">
          <el-radio-group v-model="form.status">
            <el-radio v-for="dict in dict.type.sys_data_status"
              :key="dict.value"
              :label="dict.value">{{ dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item> -->
        <el-form-item :label="$t('common.remark')">
          <el-input v-model="form.remark"
            type="textarea"
            maxlength="200"
            :placeholder="$t('common.placeholderContent')"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer"
        class="dialog-footer">
        <el-button type="primary"
          @click="submitForm">{{$t('common.ok')}}</el-button>
        <el-button @click="cancel">{{$t('common.cancel')}}</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {
  getPayeeList,
  payeeDetail,
  updatePayee,
  delPayee,
  changePayeeStatus,
} from "@/api/transaction/payee.js";
import { mapActions } from 'vuex'

export default {
  name: "payee",
  dicts: ["sys_data_status", "trans_status"],
  data () {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 角色表格数据
      peyeeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        batchId: null,
        beneName: null,
        beneAccountNumber: null,
        debitAccountNo: null,
        ifscCode: null,
        transStatus: null,
      },
      batchId: null,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userName: [
          { required: true, message: this.$t('common.ruleInputEmpty'), trigger: "blur" },
        ],
        accountName: [
          { required: true, message: this.$t('common.ruleInputEmpty'), trigger: "blur" },
        ],
      },
      auditStatus: ''
    };
  },
  created () {
    this.batchId = this.$route.params.batchId
    this.queryParams.batchId = this.batchId
    this.auditStatus = this.$route.query.auditStatus
    this.getList();
  },
  methods: {
    ...mapActions([
      'toRefresPayrollPage'
    ]),
    /** 查询列表 */
    getList () {
      this.loading = true;
      getPayeeList(this.queryParams).then(
        (response) => {
          this.peyeeList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 状态修改
    handleStatusChange (row) {
      // let text = row.status === "0" ? "启用" : "停用";
      let text = '';
      let tips = ''
      if (row.status === "1") {
        text = `${this.$t('common.statusChangesEnable')} ${row.exchangeName} ?`
        tips = this.$t('common.enabledSuccessfully')
      } else {
        text = `${this.$t('common.statusChangesDisable')} ${row.exchangeName} ?`
        tips = this.$t('common.disableSuccessfully')
      }
      this.$modal
        .confirm(text)
        .then(function () {
          return changePayeeStatus({
            status: row.status,
            payeeId: row.payeeId
          });
        })
        .then(() => {
          this.$modal.msgSuccess(tips);
        })
        .catch(function () {
          row.status = row.status === "0" ? "1" : "0";
        });
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        exchangeName: null,
        beneName: null,
        beneShortName: null,
        debitAccountNo: null,
        ifscCode: null,
        bankName: null,
        mobileNumber: null,
        emailId: null,
        transAmount: null,
        transStatus: null,
        status: null,
        productTypeCode: null,
        paymentMode: null,
        debitNarr: null,
        creditNarr: null,
      };
      this.resetForm("form");
    },
    // 返回按钮
    handleClose () {
      const obj = { path: '/transaction/payrollOrder' };
      this.$tab.closeOpenPage(obj);
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map((item) => item.payeeId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },

    /** 新增按钮操作 */
    // handleAdd () {
    //   this.reset();
    //   this.open = true;
    //   this.title = this.$t('peyee.addAccount');
    // },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset();
      const payeeId = row.payeeId || this.ids;
      payeeDetail(payeeId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = this.$t('peyee.editPayee');
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          updatePayee(this.form).then((response) => {
            this.$modal.msgSuccess(this.$t('common.editSuccessfully'));
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const payeeIds = row.payeeId || this.ids;
      this.$modal
        // .confirm('是否确认删除账号为"' + payeeIds + '"的数据项？')
        .confirm(this.$t('common.deleteDataTips'))
        .then(function () {
          return delPayee(payeeIds);
        })
        .then(() => {
          this.getList();
          let times = new Date().getTime() + ''
          this.$store.dispatch("common/toRefresPayrollPage", times)
          this.$modal.msgSuccess(this.$t('common.successfullyDeleted'));
        })
        .catch(() => { });
    }
  },
};
</script>
